/***********************************************************************************************************************
* DISCLAIMER
* This software is supplied by Renesas Electronics Corporation and is only intended for use with Renesas products.
* No other uses are authorized. This software is owned by Renesas Electronics Corporation and is protected under all
* applicable laws, including copyright laws. 
* THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIESREGARDING THIS SOFTWARE, WHETHER EXPRESS, IMPLIED
* OR STATUTORY, INCLUDING BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NON-INFRINGEMENT.  ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED.TO THE MAXIMUM EXTENT PERMITTED NOT PROHIBITED BY
* LAW, NEITHER RENESAS ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES SHALL BE LIABLE FOR ANY DIRECT,
* INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY REASON RELATED TO THIS SOFTWARE, EVEN IF RENESAS OR
* ITS AFFILIATES HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
* Renesas reserves the right, without notice, to make changes to this software and to discontinue the availability 
* of this software. By using this software, you agree to the additional terms and conditions found by accessing the 
* following link:
* http://www.renesas.com/disclaimer
*
* Copyright (C) 2015, 2016 Renesas Electronics Corporation. All rights reserved.
***********************************************************************************************************************/

/***********************************************************************************************************************
* File Name    : r_cg_sci.c
* Version      : Code Generator for RX23T V1.00.04.02 [29 Nov 2016]
* Device(s)    : R5F523T5AxFM
* Tool-Chain   : CCRX
* Description  : This file implements device driver for SCI module.
* Creation Date: 2017/8/2
***********************************************************************************************************************/

/***********************************************************************************************************************
Pragma directive
***********************************************************************************************************************/
/* Start user code for pragma. Do not edit comment generated here */
/* End user code. Do not edit comment generated here */

/***********************************************************************************************************************
Includes
***********************************************************************************************************************/
#include "r_cg_macrodriver.h"
#include "r_cg_sci.h"
/* Start user code for include. Do not edit comment generated here */
#include "all.h"
/* End user code. Do not edit comment generated here */
#include "r_cg_userdefine.h"

/***********************************************************************************************************************
Global variables and functions
***********************************************************************************************************************/
uint8_t * gp_sci1_tx_address;               /* SCI1 transmit buffer address */
uint16_t  g_sci1_tx_count;                  /* SCI1 transmit data number */
uint8_t * gp_sci1_rx_address;               /* SCI1 receive buffer address */
uint16_t  g_sci1_rx_count;                  /* SCI1 receive data number */
uint16_t  g_sci1_rx_length;                 /* SCI1 receive data length */
uint8_t * gp_sci5_tx_address;               /* SCI5 transmit buffer address */
uint16_t  g_sci5_tx_count;                  /* SCI5 transmit data number */
uint8_t * gp_sci5_rx_address;               /* SCI5 receive buffer address */
uint16_t  g_sci5_rx_count;                  /* SCI5 receive data number */
uint16_t  g_sci5_rx_length;                 /* SCI5 receive data length */
/* Start user code for global. Do not edit comment generated here */
extern uint8 U1TxBuf[U1_TX_BUF_LEN] = {0};  // 串口 1 发送缓存区
extern uint8 U1RxBuf[U1_RX_BUF_LEN] = {0};  // 串口 1 接收缓存区


extern uint8 U5TxBuf[U5_TX_BUF_LEN] = {0};  // 串口 2 发送缓存区
extern uint8 U5RxBuf[U5_RX_BUF_LEN] = {0};  // 串口 2 接收缓存区

extern uint8 IORxBuf[IO_RX_BUF_LEN] = {0};  // 串口 IO 接收缓存区


extern YesNo_t U1_Rx_End = No;              // 串口 1 是否接收完毕指定数据
extern YesNo_t U1_Tx_End = Yes;             // 串口 1 是否发送完毕指定数据
extern YesNo_t U5_Rx_End = No;              // 串口 2 是否接收完毕指定数据
extern YesNo_t U5_Tx_End = Yes;             // 串口 2 是否发送完毕指定数据
uint8_t gp_sci1_rx_address_temp;
uint8_t gp_sci5_rx_address_temp;
/* End user code. Do not edit comment generated here */

/***********************************************************************************************************************
* Function Name: R_SCI1_Create
* Description  : This function initializes SCI1.
* Arguments    : None
* Return Value : None
***********************************************************************************************************************/
void R_SCI1_Create(void)
{
    /* Cancel SCI1 module stop state */
    MSTP(SCI1) = 0U;

    /* Set interrupt priority */
    IPR(SCI1, ERI1) = _0F_SCI_PRIORITY_LEVEL15;

    /* Clear the control register */
    SCI1.SCR.BYTE = 0x00U;

    /* Set clock enable */
    SCI1.SCR.BYTE = _00_SCI_INTERNAL_SCK_UNUSED;

    /* Clear the SIMR1.IICM, SPMR.CKPH, and CKPOL bit, and set SPMR */
    SCI1.SIMR1.BIT.IICM = 0U;
    SCI1.SPMR.BYTE = _00_SCI_RTS | _00_SCI_CLOCK_NOT_INVERTED | _00_SCI_CLOCK_NOT_DELAYED;

    /* Set control registers */
    SCI1.SMR.BYTE = _00_SCI_CLOCK_PCLK | _00_SCI_STOP_1 | _00_SCI_PARITY_DISABLE | _00_SCI_DATA_LENGTH_8 | 
                    _00_SCI_MULTI_PROCESSOR_DISABLE | _00_SCI_ASYNCHRONOUS_MODE;
    SCI1.SCMR.BYTE = _00_SCI_SERIAL_MODE | _00_SCI_DATA_INVERT_NONE | _00_SCI_DATA_LSB_FIRST | 
                     _10_SCI_DATA_LENGTH_8_OR_7 | _62_SCI_SCMR_DEFAULT;
    SCI1.SEMR.BYTE = _00_SCI_LOW_LEVEL_START_BIT | _00_SCI_NOISE_FILTER_DISABLE | _10_SCI_8_BASE_CLOCK | 
                     _40_SCI_BAUDRATE_DOUBLE | _00_SCI_BIT_MODULATION_DISABLE;

    /* Set bitrate */
    SCI1.BRR = 0x2AU;

    /* Set RXD1 pin */
    MPC.PD5PFS.BYTE = 0x0AU;
    PORTD.PMR.BYTE |= 0x20U;

    /* Set TXD1 pin */
    MPC.PD3PFS.BYTE = 0x0AU;
    PORTD.PODR.BYTE |= 0x08U;
    PORTD.PDR.BYTE |= 0x08U;
    PORTD.PMR.BYTE |= 0x08U;
}
/***********************************************************************************************************************
* Function Name: R_SCI1_Start
* Description  : This function starts SCI1.
* Arguments    : None
* Return Value : None
***********************************************************************************************************************/
void R_SCI1_Start(void)
{
    /* Clear interrupt flag */
    IR(SCI1, TXI1) = 0U;
    IR(SCI1, RXI1) = 0U;

    /* Enable SCI interrupt */
    IEN(SCI1, TXI1) = 1U;
    IEN(SCI1, TEI1) = 1U;
    IEN(SCI1, RXI1) = 1U;
    IEN(SCI1, ERI1) = 1U;
}
/***********************************************************************************************************************
* Function Name: R_SCI1_Stop
* Description  : This function stops SCI1.
* Arguments    : None
* Return Value : None
***********************************************************************************************************************/
void R_SCI1_Stop(void)
{
    /* Set TXD1 pin */
    PORTD.PMR.BYTE &= 0xF7U;
    SCI1.SCR.BIT.TE = 0U;      /* Disable serial transmit */
    SCI1.SCR.BIT.RE = 0U;      /* Disable serial receive */

    /* Disable SCI interrupt */
    SCI1.SCR.BIT.TIE = 0U;     /* Disable TXI interrupt */
    SCI1.SCR.BIT.RIE = 0U;     /* Disable RXI and ERI interrupt */
    IEN(SCI1, TXI1) = 0U;
    IEN(SCI1, TEI1) = 0U;
    IR(SCI1, TXI1) = 0U;
    IEN(SCI1, RXI1) = 0U;
    IEN(SCI1, ERI1) = 0U;
    IR(SCI1, RXI1) = 0U;
}
/***********************************************************************************************************************
* Function Name: R_SCI1_Serial_Receive
* Description  : This function receives SCI1 data.
* Arguments    : rx_buf -
*                    receive buffer pointer (Not used when receive data handled by DTC or DMAC)
*                rx_num -
*                    buffer size (Not used when receive data handled by DTC or DMAC)
* Return Value : status -
*                    MD_OK or MD_ARGERROR
***********************************************************************************************************************/
MD_STATUS R_SCI1_Serial_Receive(uint8_t * const rx_buf, uint16_t rx_num)
{
    MD_STATUS status = MD_OK;

    if (1U > rx_num)
    {
        status = MD_ARGERROR;
    }
    else
    {
        g_sci1_rx_count = 0U;
        g_sci1_rx_length = rx_num;
        gp_sci1_rx_address = rx_buf;
        SCI1.SCR.BIT.RIE = 1U;
        SCI1.SCR.BIT.RE = 1U;
    }

    return (status);
}
/***********************************************************************************************************************
* Function Name: R_SCI1_Serial_Send
* Description  : This function transmits SCI1 data.
* Arguments    : tx_buf -
*                    transfer buffer pointer (Not used when transmit data handled by DTC)
*                tx_num -
*                    buffer size (Not used when transmit data handled by DTC or DMAC)
* Return Value : status -
*                    MD_OK or MD_ARGERROR
***********************************************************************************************************************/
MD_STATUS R_SCI1_Serial_Send(uint8_t * const tx_buf, uint16_t tx_num)
{
    MD_STATUS status = MD_OK;

    if (1U > tx_num)
    {
        status = MD_ARGERROR;
    }
    else
    {
        gp_sci1_tx_address = tx_buf;
        g_sci1_tx_count = tx_num;

        /* Set TXD1 pin */
        PORTD.PMR.BYTE |= 0x08U;
        SCI1.SCR.BIT.TIE = 1U;
        SCI1.SCR.BIT.TE = 1U;
    }

    return (status);
}
/***********************************************************************************************************************
* Function Name: R_SCI5_Create
* Description  : This function initializes SCI5.
* Arguments    : None
* Return Value : None
***********************************************************************************************************************/
void R_SCI5_Create(void)
{
    /* Cancel SCI5 module stop state */
    MSTP(SCI5) = 0U;

    /* Set interrupt priority */
    IPR(SCI5, ERI5) = _0F_SCI_PRIORITY_LEVEL15;

    /* Clear the control register */
    SCI5.SCR.BYTE = 0x00U;

    /* Set clock enable */
    SCI5.SCR.BYTE = _00_SCI_INTERNAL_SCK_UNUSED;

    /* Clear the SIMR1.IICM, SPMR.CKPH, and CKPOL bit, and set SPMR */
    SCI5.SIMR1.BIT.IICM = 0U;
    SCI5.SPMR.BYTE = _00_SCI_RTS | _00_SCI_CLOCK_NOT_INVERTED | _00_SCI_CLOCK_NOT_DELAYED;

    /* Set control registers */
    SCI5.SMR.BYTE = _00_SCI_CLOCK_PCLK | _00_SCI_STOP_1 | _00_SCI_PARITY_DISABLE | _00_SCI_DATA_LENGTH_8 | 
                    _00_SCI_MULTI_PROCESSOR_DISABLE | _00_SCI_ASYNCHRONOUS_MODE;
    SCI5.SCMR.BYTE = _00_SCI_SERIAL_MODE | _00_SCI_DATA_INVERT_NONE | _00_SCI_DATA_LSB_FIRST | 
                     _10_SCI_DATA_LENGTH_8_OR_7 | _62_SCI_SCMR_DEFAULT;
    SCI5.SEMR.BYTE = _00_SCI_LOW_LEVEL_START_BIT | _00_SCI_NOISE_FILTER_DISABLE | _10_SCI_8_BASE_CLOCK | 
                     _00_SCI_BAUDRATE_SINGLE | _00_SCI_BIT_MODULATION_DISABLE;

    /* Set bitrate */
    SCI5.BRR = 0x40U;

    /* Set RXD5 pin */
    MPC.PB1PFS.BYTE = 0x0AU;
    PORTB.PMR.BYTE |= 0x02U;

    /* Set TXD5 pin */
    MPC.PB2PFS.BYTE = 0x0AU;
    PORTB.PODR.BYTE |= 0x04U;
    PORTB.PDR.BYTE |= 0x04U;
    PORTB.PMR.BYTE |= 0x04U;
}
/***********************************************************************************************************************
* Function Name: R_SCI5_Start
* Description  : This function starts SCI5.
* Arguments    : None
* Return Value : None
***********************************************************************************************************************/
void R_SCI5_Start(void)
{
    /* Clear interrupt flag */
    IR(SCI5, TXI5) = 0U;
    IR(SCI5, RXI5) = 0U;

    /* Enable SCI interrupt */
    IEN(SCI5, TXI5) = 1U;
    IEN(SCI5, TEI5) = 1U;
    IEN(SCI5, RXI5) = 1U;
    IEN(SCI5, ERI5) = 1U;
}
/***********************************************************************************************************************
* Function Name: R_SCI5_Stop
* Description  : This function stops SCI5.
* Arguments    : None
* Return Value : None
***********************************************************************************************************************/
void R_SCI5_Stop(void)
{
    /* Set TXD5 pin */
    PORTB.PMR.BYTE &= 0xFBU;
    SCI5.SCR.BIT.TE = 0U;      /* Disable serial transmit */
    SCI5.SCR.BIT.RE = 0U;      /* Disable serial receive */

    /* Disable SCI interrupt */
    SCI5.SCR.BIT.TIE = 0U;     /* Disable TXI interrupt */
    SCI5.SCR.BIT.RIE = 0U;     /* Disable RXI and ERI interrupt */
    IEN(SCI5, TXI5) = 0U;
    IEN(SCI5, TEI5) = 0U;
    IR(SCI5, TXI5) = 0U;
    IEN(SCI5, RXI5) = 0U;
    IEN(SCI5, ERI5) = 0U;
    IR(SCI5, RXI5) = 0U;
}
/***********************************************************************************************************************
* Function Name: R_SCI5_Serial_Receive
* Description  : This function receives SCI5 data.
* Arguments    : rx_buf -
*                    receive buffer pointer (Not used when receive data handled by DTC or DMAC)
*                rx_num -
*                    buffer size (Not used when receive data handled by DTC or DMAC)
* Return Value : status -
*                    MD_OK or MD_ARGERROR
***********************************************************************************************************************/
MD_STATUS R_SCI5_Serial_Receive(uint8_t * const rx_buf, uint16_t rx_num)
{
    MD_STATUS status = MD_OK;

    if (1U > rx_num)
    {
        status = MD_ARGERROR;
    }
    else
    {
        g_sci5_rx_count = 0U;
        g_sci5_rx_length = rx_num;
        gp_sci5_rx_address = rx_buf;
        SCI5.SCR.BIT.RIE = 1U;
        SCI5.SCR.BIT.RE = 1U;
    }

    return (status);
}
/***********************************************************************************************************************
* Function Name: R_SCI5_Serial_Send
* Description  : This function transmits SCI5 data.
* Arguments    : tx_buf -
*                    transfer buffer pointer (Not used when transmit data handled by DTC)
*                tx_num -
*                    buffer size (Not used when transmit data handled by DTC or DMAC)
* Return Value : status -
*                    MD_OK or MD_ARGERROR
***********************************************************************************************************************/
MD_STATUS R_SCI5_Serial_Send(uint8_t * const tx_buf, uint16_t tx_num)
{
    MD_STATUS status = MD_OK;

    if (1U > tx_num)
    {
        status = MD_ARGERROR;
    }
    else
    {
        gp_sci5_tx_address = tx_buf;
        g_sci5_tx_count = tx_num;

        /* Set TXD5 pin */
        PORTB.PMR.BYTE |= 0x04U;
        SCI5.SCR.BIT.TIE = 1U;
        SCI5.SCR.BIT.TE = 1U;
    }

    return (status);
}

/* Start user code for adding. Do not edit comment generated here */

void R_SCI1_Serial_Send_Char(uint8_t  tx_char)
{
		while(!SCI1.SSR.BIT.TEND)//一个字节传输完成了

		PORTD.PMR.BYTE |= 0x08U;
        SCI1.SCR.BIT.TIE = 0U;
        SCI1.SCR.BIT.TEIE = 0U;
        SCI1.SCR.BIT.TE = 1U;
        SCI1.TDR = tx_char;
}

void R_SCI1_Sent_Init32(int32_t data,int8_t mode)
{
    int32_t data_abs=ABS(data);
    if(data<0)
    	R_SCI1_Serial_Send_Char('-');
    R_SCI1_Serial_Send_Char(data_abs/1000000000+0x30);
    R_SCI1_Serial_Send_Char(data_abs%1000000000/100000000+0x30);
    R_SCI1_Serial_Send_Char(data_abs%1000000000%100000000/10000000+0x30);
    R_SCI1_Serial_Send_Char(data_abs%1000000000%100000000%10000000/1000000+0x30);
    R_SCI1_Serial_Send_Char(data_abs%1000000000%100000000%10000000%1000000/100000+0x30);
    R_SCI1_Serial_Send_Char(data_abs%1000000000%100000000%10000000%1000000%100000/10000+0x30);
    R_SCI1_Serial_Send_Char(data_abs%1000000000%100000000%10000000%1000000%100000%10000/1000+0x30);
    R_SCI1_Serial_Send_Char(data_abs%1000000000%100000000%10000000%1000000%100000%10000%1000/100+0x30);
    R_SCI1_Serial_Send_Char(data_abs%1000000000%100000000%10000000%1000000%100000%10000%1000%100/10+0x30);
    R_SCI1_Serial_Send_Char(data_abs%1000000000%100000000%10000000%1000000%100000%10000%1000%100%10+0x30);

    if(mode==1)R_SCI1_Serial_Send_Char('\n');
}

void R_SCI5_Serial_Send_Char(uint8_t  tx_char)
{
		while(!SCI5.SSR.BIT.TEND)//一个字节传输完成了

		PORTD.PMR.BYTE |= 0x08U;
        SCI5.SCR.BIT.TIE = 0U;
        SCI5.SCR.BIT.TEIE = 0U;
        SCI5.SCR.BIT.TE = 1U;
        SCI5.TDR = tx_char;
}

void R_SCI5_Sent_Init32(int32_t data,int8_t mode)
{
    int32_t data_abs=ABS(data);
    if(data<0)
    	R_SCI5_Serial_Send_Char('-');
    R_SCI5_Serial_Send_Char(data_abs/1000000000+0x30);
    R_SCI5_Serial_Send_Char(data_abs%1000000000/100000000+0x30);
    R_SCI5_Serial_Send_Char(data_abs%1000000000%100000000/10000000+0x30);
    R_SCI5_Serial_Send_Char(data_abs%1000000000%100000000%10000000/1000000+0x30);
    R_SCI5_Serial_Send_Char(data_abs%1000000000%100000000%10000000%1000000/100000+0x30);
    R_SCI5_Serial_Send_Char(data_abs%1000000000%100000000%10000000%1000000%100000/10000+0x30);
    R_SCI5_Serial_Send_Char(data_abs%1000000000%100000000%10000000%1000000%100000%10000/1000+0x30);
    R_SCI5_Serial_Send_Char(data_abs%1000000000%100000000%10000000%1000000%100000%10000%1000/100+0x30);
    R_SCI5_Serial_Send_Char(data_abs%1000000000%100000000%10000000%1000000%100000%10000%1000%100/10+0x30);
    R_SCI5_Serial_Send_Char(data_abs%1000000000%100000000%10000000%1000000%100000%10000%1000%100%10+0x30);

    if(mode==1)R_SCI5_Serial_Send_Char('\n');
}
// 串口接收缓存区内容初始化
/*******************大飞机移植函数开始*******************************/

extern void Usart_Rx_Buf_Init(void)
{
    uint16 i;

    for(i=0; i<U1_RX_BUF_LEN; i++)
        U1RxBuf[i] = 0;

    for(i=0; i<U5_RX_BUF_LEN; i++)
        U5RxBuf[i] = 0;

}
// 指定串口发送一个字节数据
extern void Usart_Send_Byte(UART_HandleTypeDef huart, uint8 Byte)
{
    uint8* pTxBuf;

    if(USART1 == huart)
    {
        pTxBuf = U1TxBuf;

        while(No == U1_Tx_End)
            ;
        U1_Tx_End = No;
        pTxBuf[0] = Byte;
        R_SCI1_Serial_Send(pTxBuf,1);
    }
    else if(USART5 == huart)
    {
        pTxBuf = U5TxBuf;

        while(No == U5_Tx_End)
            ;
        U5_Tx_End = No;
        pTxBuf[0] = Byte;
        R_SCI5_Serial_Send(pTxBuf,1);
    }
}
// 指定的串口 发送指定内容数据
extern void Usart_Send_Data(UART_HandleTypeDef huart, uint8* pTx, uint8 len)
{
    uint16 i;
    uint8* pTxBuf;

    if(USART1 == huart)
    {
        pTxBuf = U1TxBuf;
        while(No == U1_Tx_End)
            ;
        U1_Tx_End = No;
        for(i=0; i<len; i++)
            pTxBuf[i] = pTx[i];
        R_SCI1_Serial_Send(pTxBuf,len);
    }
    else if(USART5 == huart)
    {
        pTxBuf = U5TxBuf;
        while(No == U5_Tx_End)
            ;
        U5_Tx_End = No;
        for(i=0; i<len; i++)
            pTxBuf[i] = pTx[i];
        R_SCI5_Serial_Send(pTxBuf,len);
    }
}
// 指定的串口发送字符串数据
extern void Usart_Send_String(UART_HandleTypeDef huart, const char* pTx)
{
    uint16 i;
    uint8* pTxBuf;

    if(USART1 == huart)
    {
        pTxBuf = U1TxBuf;

        while(No == U1_Tx_End)
            ;
        U1_Tx_End = No;
        for(i=0; i<strlen(pTx); i++)
            pTxBuf[i] = pTx[i];
        R_SCI1_Serial_Send(pTxBuf,strlen(pTx));

    }
    else if(USART5 == huart)
    {
        pTxBuf = U5TxBuf;

        while(No == U5_Tx_End)
            ;
        U5_Tx_End = No;
        for(i=0; i<strlen(pTx); i++)
            pTxBuf[i] = pTx[i];
        R_SCI5_Serial_Send(pTxBuf,strlen(pTx));
    }
}
// 指定的串口接收指定长度的数据，存放在指定的空间
extern void Usart_Receive_IT(UART_HandleTypeDef huart, uint8 *pRx, uint16_t len)
{
    uint8* pRxBuf;

    if(USART1 == huart)
    {
        pRxBuf = U1RxBuf;
        U1_Rx_End = No;
        R_SCI1_Serial_Receive(pRxBuf,len);
    }
    else if(USART5 == huart)
    {
        pRxBuf = U5RxBuf;
        U5_Rx_End = No;
        R_SCI5_Serial_Receive(pRxBuf,len);
    }

}
// 指定的串口发送缓存区已经填充过内容 按照指定的长度发送数据
extern void Usart_Send_TxBuf_Filled_Data(UART_HandleTypeDef huart, uint16_t len)
{
    uint8* pTxBuf;

    if(USART1 == huart)
    {
        pTxBuf = U1TxBuf;

        while(No == U1_Tx_End)
            ;
        U1_Tx_End = No;
        R_SCI1_Serial_Send(pTxBuf,len);
    }
    else if(USART5 == huart)
    {
        pTxBuf = U5TxBuf;

        while(No == U5_Tx_End)
            ;
        U5_Tx_End = No;
        R_SCI5_Serial_Send(pTxBuf,len);
    }
}
/*******************大飞机移植函数结束*******************************/

/* End user code. Do not edit comment generated here */
